package com.seek.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.seek.eduservice.entity.EduSubject;
import com.seek.eduservice.entity.excel.SubjectData;
import com.seek.eduservice.entity.subject.Subject;
import com.seek.eduservice.mapper.EduSubjectMapper;
import com.seek.eduservice.service.EduSubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author wml
 * @since 2022-01-15
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    // 添加课程分类
    @Override
    public void saveSubject(List<SubjectData> list) {
        QueryWrapper<EduSubject> queryWrapper = new QueryWrapper();
        EduSubject oneSubject = null;
        EduSubject twoSubject = null;
        for (SubjectData subjectData : list) {
            if (subjectData.getOneSubjectName() == null || subjectData.getTwoSubjectName() == null) {
                continue;
            }
            queryWrapper.eq("title", subjectData.getOneSubjectName());
            queryWrapper.eq("parent_id", "0");
            oneSubject = baseMapper.selectOne(queryWrapper);
            if (oneSubject == null) {
                oneSubject = new EduSubject();
                oneSubject.setTitle(subjectData.getOneSubjectName());
                oneSubject.setParentId("0");
                baseMapper.insert(oneSubject);
            }
            queryWrapper.clear();
            queryWrapper.eq("title", subjectData.getTwoSubjectName());
            queryWrapper.eq("parent_id", oneSubject.getId());
            twoSubject = baseMapper.selectOne(queryWrapper);
            if (twoSubject == null) {
                twoSubject = new EduSubject();
                twoSubject.setTitle(subjectData.getTwoSubjectName());
                twoSubject.setParentId(oneSubject.getId());
                baseMapper.insert(twoSubject);
            }
            queryWrapper.clear();
        }
    }

    // 课程分类列表（树形）：方法一（查询多次数据库，支持两级分类，不建议使用）
//    @Override
//    public List<Subject> getAll() {
//        List<Subject> results = new ArrayList<>();
//        QueryWrapper<EduSubject> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("parent_id", "0");
//        List<EduSubject> eduSubjects = baseMapper.selectList(queryWrapper);
//        for (EduSubject eduSubject : eduSubjects) {
//            Subject subject = new Subject();
//            subject.setId(eduSubject.getId());
//            subject.setTitle(eduSubject.getTitle());
//            subject.setChildren(null);
//            results.add(subject);
//        }
//        for (Subject subject : results) {
//            queryWrapper.clear();
//            queryWrapper.eq("parent_id", subject.getId());
//            eduSubjects = baseMapper.selectList(queryWrapper);
//            if (eduSubjects != null) {
//                subject.setChildren(new ArrayList<>());
//                for (EduSubject eduSubject : eduSubjects) {
//                    Subject subSubject = new Subject();
//                    subSubject.setId(eduSubject.getId());
//                    subSubject.setTitle(eduSubject.getTitle());
//                    subSubject.setChildren(null);
//                    subject.getChildren().add(subSubject);
//                }
//            }
//        }
//        return results;
//    }

    // 课程分类列表（树形）：方法二（方法一的基础上，使用BeanUtils封装数据，支持两级分类）
//    @Override
//    public List<Subject> getAll() {
//        List<Subject> results = new ArrayList<>();
//        QueryWrapper<EduSubject> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("parent_id", "0");
//        List<EduSubject> eduSubjects = baseMapper.selectList(queryWrapper);
//        for (EduSubject eduSubject : eduSubjects) {
//            Subject subject = new Subject();
//            BeanUtils.copyProperties(eduSubject, subject);
//            results.add(subject);
//        }
//        for (Subject subject : results) {
//            queryWrapper.clear();
//            queryWrapper.eq("parent_id", subject.getId());
//            eduSubjects = baseMapper.selectList(queryWrapper);
//            if (eduSubjects != null) {
//                subject.setChildren(new ArrayList<>());
//                for (EduSubject eduSubject : eduSubjects) {
//                    Subject subSubject = new Subject();
//                    BeanUtils.copyProperties(eduSubject, subSubject);
//                    subject.getChildren().add(subSubject);
//                }
//            }
//        }
//        return results;
//    }

    // 课程分类列表（树形）：方法三（查询两次数据库，支持两级分类）
//    @Override
//    public List<Subject> getAll() {
//        List<Subject> results = new ArrayList<>();
//        List<Subject> subSubjects = null;
//        QueryWrapper<EduSubject> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("parent_id", "0");
//        List<EduSubject> eduSubjects = baseMapper.selectList(queryWrapper);
//        queryWrapper.clear();
//        queryWrapper.ne("parent_id", "0");
//        List<EduSubject> eduSubSubjects = baseMapper.selectList(queryWrapper);
//        for (EduSubject eduSubject : eduSubjects) {
//            Subject subject = new Subject();
//            BeanUtils.copyProperties(eduSubject, subject);
//            subSubjects = new ArrayList<>();
//            for (EduSubject eduSubSubject : eduSubSubjects) {
//                if (eduSubSubject.getParentId().equals(subject.getId())) {
//                    Subject subSubject = new Subject();
//                    BeanUtils.copyProperties(eduSubSubject, subSubject);
//                    subSubjects.add(subSubject);
//                }
//            }
//            subject.setChildren(subSubjects);
//            results.add(subject);
//        }
//        return results;
//    }

    // 课程分类列表（树形）：方法四（查询一次数据库，并支持多级分类）
    @Override
    public List<Subject> getAll() {
        List<Subject> results = new ArrayList<>();
        Subject subject = null;
        EduSubject eduSubject = null;
        List<EduSubject> eduSubjects = baseMapper.selectList(null);
        for (int i = 0; i < eduSubjects.size(); i++) {
            eduSubject = eduSubjects.get(i);
            if (eduSubject.getParentId().equals("0")) {
                subject = new Subject();
                BeanUtils.copyProperties(eduSubject, subject);
                results.add(subject);
                eduSubjects.remove(eduSubject);
                i--;
            }
        }
        List<Subject> tempParent = results;
        while (true) {
            getSubjectsByParentId(tempParent, eduSubjects);
            if (eduSubjects.size() > 0) {
                List<Subject> temp = new ArrayList<>();
                for (Subject parentSubject : tempParent) {
                    if (parentSubject.getChildren() != null) {
                        temp.addAll(parentSubject.getChildren());
                    }
                }
                tempParent = temp;
            } else {
                break;
            }
        }
        return results;
    }

    public void getSubjectsByParentId(List<Subject> parentSubjects, List<EduSubject> eduSubjects) {
        Subject subSubject = null;
        EduSubject eduSubject = null;
        for (Subject parentSubject : parentSubjects) {
            List<Subject> subSubjects = new ArrayList<>();
            for (int i = 0; i < eduSubjects.size(); i++) {
                eduSubject = eduSubjects.get(i);
                if (eduSubject.getParentId().equals(parentSubject.getId())) {
                    subSubject = new Subject();
                    BeanUtils.copyProperties(eduSubject, subSubject);
                    subSubjects.add(subSubject);
                    eduSubjects.remove(eduSubject);
                    i--;
                }
            }
            parentSubject.setChildren(subSubjects);
        }
    }

}
